﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iOak.Domain.Resumes;
using iOak.Business.Resumes;
using iOak.Business.Directory;
using iOak.Domain.Directory;
using iOak.Business.Users;
using iOak.Business.Campaigns;
using System.Transactions;

namespace iOak.Web.Administration
{
    public partial class CompanyIndex : BasePage
    {
        private CompanyService _companyService = new CompanyService();
        private PicklistValueService _picklistValueService = new PicklistValueService();

        #region 页面事件
        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            pagerControl.PageIndexChange += new Modules.PagerControl.OnPageIndexChangeHandler(pagerControl_PageIndexChange);
            if (!Page.IsPostBack)
            {
                BindCompanyList();
            }
        }

        protected void pagerControl_PageIndexChange(object serder, EventArgs e)
        {
            BindCompanyList();
        }

        /// <summary>
        /// gvCompany行绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvCompany_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Company company = e.Row.DataItem as Company;
                if (company.CompanyTypeCode.HasValue)
                {
                    int picklistValueId = 100 * (int)PickListEnum.COMPANYTYPE + company.CompanyTypeCode.Value;
                    PicklistValue picklistValue = _picklistValueService.GetById(picklistValueId);
                    Label lblCompanyType = e.Row.FindControl("lblCompanyType") as Label;
                    lblCompanyType.Text = picklistValue.Name;
                }
                Label lblStatus = e.Row.FindControl("lblStatus") as Label;
                Panel pnlOptionMenu = e.Row.FindControl("pnlOptionMenu") as Panel;
                PlaceHolder phToActive = pnlOptionMenu.FindControl("phToActive") as PlaceHolder;
                PlaceHolder phToNoActive = pnlOptionMenu.FindControl("phToNoActive") as PlaceHolder;
                if (!company.IsActive.HasValue)
                {
                    lblStatus.Text = "未审核";
                }
                else if (company.IsActive.Value)
                {
                    phToActive.Visible = false;
                    lblStatus.Text = "审核通过";
                }
                else
                {
                    phToNoActive.Visible = false;
                    lblStatus.Text = "审核不通过";
                }

                HyperLink hlCompanyName = e.Row.FindControl("hlCompanyName") as HyperLink;
                hlCompanyName.Text = company.CompanyName;
                hlCompanyName.NavigateUrl = "javascript:modifyClick('" + company.CompanyId.ToString() + "')";
            }
        }

        /// <summary>
        /// 检索按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFind_Click(object sender, EventArgs e)
        {
            gvCompany.PageIndex = 0;
            BindCompanyList();
        }

        protected void btnPostBack_Click(object sender, EventArgs e)
        {
            Guid id;
            switch (hidEventName.Value)
            {
                case "active":
                    id = Guid.Parse(hidEventArguments.Value);
                    SetStatus(true, id);
                    break;
                case "noActive":
                    id = Guid.Parse(hidEventArguments.Value);
                    SetStatus(false, id);
                    break;
                case "delete":
                    id = Guid.Parse(hidEventArguments.Value);
                    DeleteCompany(id);
                    break;
            }
        }
        #endregion

        #region 处理函数
        /// <summary>
        /// 绑定Company列表
        /// </summary>
        private void BindCompanyList()
        {
            bool? isActive = null;
            if (ddlStatus.SelectedIndex != 0)
            {
                isActive = ddlStatus.SelectedValue.Equals("0");
            }
            //检索符合条件的company
            List<Company> companyList = _companyService.Select(txtCompanyName.Text.Trim(), isActive, txtKeyword.Text.Trim(), 1);
            pagerControl.BindData(gvCompany, companyList);
        }

        private void SetStatus(bool active, Guid id)
        {
            Company company = _companyService.Retrieve(id);
            company.IsActive = active;
            _companyService.Update(company);
            BindCompanyList();
        }

        private void DeleteCompany(Guid id)
        {
            using (TransactionScope ts = new TransactionScope()) 
            {
                //删除公司的人员信息
                UserService userService = new UserService();
                var userList = userService.GetListByCompanyId(id);
                userList.ForEach(item => userService.Delete(item.UserId));
                //删除候选人信息
                ApplicationCandidateService applicationCandidateService = new ApplicationCandidateService();
                var applicationCandidateList = applicationCandidateService.GetListByCompanyId(id);
                applicationCandidateList.ForEach(item => applicationCandidateService.Delete(item.ApplicationCandidateId));
                //删除申请书
                ApplicationService applicationService = new ApplicationService();
                var applicationList = applicationService.GetListByCompanyId(id);
                applicationList.ForEach(item => applicationService.Delete(item.ApplicationId));
                //删除公司信息
                _companyService.Delete(id);
                ts.Complete();
            }
            BindCompanyList();
        }

        #endregion
    }
}